using System;

namespace IValidate
{
	public class ValidateLengthAttribute : ModelValidatorBaseImpl
	{
		public int MinLength { get; private set; }
		public int MaxLength { get; private set; }

		public ValidateLengthAttribute(int minLength, int maxLength, string errorMessage, bool required)
			: base(errorMessage, required)
		{
			MinLength = minLength;
			MaxLength = maxLength;
		}

		public override bool Validate(object value)
		{
			if (this.Required && value == null) return false;
			int length = value.ToString().Length;
			if (this.Required && (length < this.MinLength || length > this.MaxLength)) return false;
			return true;
		}
	}
}
